package MineGod;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.opengl.Texture;

public abstract class ProjectileEntity {

	public double xPos;
	public double yPos;
	protected double width;
	protected double height;
	protected double maskWidth;
	protected double maskHeight;
	
	protected double xSpeed;
	protected double ySpeed;
	protected double speed;
	
	protected double maxAge;
	protected double age;
	
	protected boolean dead;
	
	protected double baseDamage;
	
	public double zIndex = -3;
	
	
	Rectangle mask;
	
	ProjectileType type;
	
	public ProjectileEntity(ProjectileType tempType, double x, double y){
		xPos = x;
		yPos = y;
		type = tempType;
		dead = false;
		/* Set the following variables in subclasses' constructors:
		 * super(type, x, y);
		 * width =
		 * height =
		 * xSpeed = 
		 * ySpeed = 
		 * speed = 
		 * maxAge = 
		 * age = 0 (most likely..)
		 * baseDamage = 
		 * mask = 
		 */
	}
	
	public abstract void doLogic(int delta, Chunk prevChunk, Chunk currChunk, Chunk nextChunk);
	
	public abstract void render(double worldPos);
	
	public void handleCollisions(int delta, Chunk prevChunk, Chunk currChunk, Chunk nextChunk){
		Rectangle newMask = getNextMask((float)xSpeed*delta, 0f);
		CollisionGroup sideBlocks = getSideBlocks(prevChunk, currChunk, nextChunk, newMask);
		for (BlockMask blockMask: sideBlocks.orderedArray){
			if (blockMask.block != null && blockMask.block.isSolid && Utils.intersects(blockMask.mask, newMask)){
				//KABOOM
				blowUp(blockMask);
				this.dead = true;
				break;
			}
		}
		
	}
	
	
	public CollisionGroup getSideBlocks(Chunk prevChunk, Chunk currChunk, Chunk nextChunk, Shape nextMask){
		double relativeXPos = nextMask.getMinX() % Chunk.CHUNK_PIXEL_WIDTH;
		if (relativeXPos < Chunk.CHUNK_PIXEL_WIDTH - mask.getWidth()){
			return currChunk.getCollidedSideBlocks(nextMask.getMinX(), nextMask.getMinY(), nextMask.getWidth(), nextMask.getHeight());
		} else {
			CollisionGroup group1 = currChunk.getCollidedLeftSideBlocks(nextMask.getMinX(), nextMask.getMinY(), nextMask.getWidth(), nextMask.getHeight());
			CollisionGroup group2 = nextChunk.getCollidedLeftSideBlocks(nextMask.getMinX()+Block.WIDTH, nextMask.getMinY(), nextMask.getWidth(), nextMask.getHeight());
			return new CollisionGroup(group1.topLeft, group2.topLeft, group1.bottomLeft, group2.bottomLeft);
		}
		
	}
	
	public abstract Rectangle getNextMask(float xs, float ys);
	public abstract void blowUp(BlockMask b);
	
	
	
}
